package exttable.questionnaire;

import dbutils.DbUtils;

import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ADMIN
 * Date: 27.10.2010
 * Time: 9:32:49
 * To change this template use File | Settings | File Templates.
 */
public class Questionnaire {
    JFrame frame;
     JPanel panelFlow;
    JPanel panelGrid;
//    JPanel panelGeneralQuestions;
    JPanel panelSubQuestions;
    JScrollPane jsp;

    public Questionnaire(){
        frame=new JFrame("Form");
        panelFlow = new JPanel(new FlowLayout());
        panelGrid = new JPanel(new GridLayout(11,1));
//        panelGeneralQuestions=new JPanel();
        try{
            PreparedStatement ps;
            Connection conn= DbUtils.getDBConnectionGeneral();
            ps=conn.prepareStatement("select * from dic_general_questions");
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                JPanel panelGeneralQuestions=new JPanel();
                panelGeneralQuestions.add(new JLabel(rs.getString("general_quest_id")+"  "
                        +rs.getString("discription_of_question_ru")));
               try{
                  PreparedStatement ps2;
            conn= DbUtils.getDBConnectionGeneral();
            ps2=conn.prepareStatement("select * from questions where general_quest_id="+rs.getString("general_quest_id"));
//                   System.out.println("select * from questions where general_quest_id="+rs.getString("general_quest_id"));
            ResultSet rs2 = ps2.executeQuery();
                   while(rs2.next()){
                   if(rs2.getString("subquestion_ru")==null){
                       addAnswers(panelGeneralQuestions,Integer.parseInt(rs2.getString("question_id")));
                       panelGrid.add(panelGeneralQuestions);
//                       System.out.println("size panelGeneralQuestions "+panelGeneralQuestions.getComponentCount());
//                       System.out.println("size panelGrid "+panelGrid.getComponentCount());
                       break;

                   }
                   else{
                       JPanel panel=new JPanel();
                       panel.add(new JLabel(rs2.getString("subquestion_ru")));
//                       System.out.println("****"+rs2.getString("subquestion_ru"));
                       addAnswers(panel,Integer.parseInt(rs2.getString("question_id")));
                       panelGeneralQuestions.add(panel);
                       panelGrid.add(panelGeneralQuestions);
//                       System.out.println("size panelGeneralQuestions "+panelGeneralQuestions.getComponentCount());
//                       System.out.println("size panelGrid "+panelGrid.getComponentCount());
                   }
               }
               }catch (Exception exp){
                   exp.printStackTrace();
               }
            }


                   conn.close();
        }
        catch (Exception exp){
           exp.printStackTrace();
        }




//        panelGrid.add(panelGeneralQuestions);
        panelFlow.add(panelGrid);
        JScrollPane jsp = new JScrollPane(panelFlow);
        frame.add(jsp);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(Toolkit.getDefaultToolkit().getScreenSize().width,Toolkit.getDefaultToolkit().getScreenSize().height);
//        frame.pack();
        frame.setVisible(true);

    }
    public static void addAnswers(JPanel panelGeneralQuestions ,int question_id){
        try{
       PreparedStatement psAnswers;
       Connection conn= DbUtils.getDBConnectionGeneral();
            psAnswers=conn.prepareStatement("select * from answers where question_id="+question_id);
//            System.out.println("select * from answers where question_id="+question_id);
            ResultSet rs=psAnswers.executeQuery();
            List<String> answerslist=new ArrayList<String>();

            while(rs.next()){
              answerslist.add(rs.getString("answer_name_ru"));
            }
            conn.close();
            JComboBox answerComboBox=new JComboBox(answerslist.toArray());
            panelGeneralQuestions.add(answerComboBox);
    }catch (Exception exp){
            exp.printStackTrace();
        }
    }
}
